package com.zq;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ProjectName: ybxy6
 * @Titile: CollectiondDemo
 * @Author: zqsfg
 * @Description: TODO
 * 演示collection集合
 */
public class CollectiondDemo {
    public static void main(String[] args) {
//        extracted();
        test1();
    }

    private static void extracted() {
        //        ArrayList<String> list = new ArrayList<>();
        //多态的写法
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"张三","李四","王五","赵六");
        list.forEach(System.out::println);
        System.out.println("--------------------------");

        //打乱集合顺序
        Collections.shuffle(list);
        list.forEach(System.out::println);
    }

    public static void test1(){
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list,1,2,3,4,5,6,7,8,9,10);
        Collections.sort(list);
        list.forEach(System.out::println);
        //按照倒序给集合排序;
//        Collections.sort(list, new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                //实现倒序排序
//                return o2-o1;
//            }
//        });
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                //实现倒序排序
                return o2 - o1;
            }
        });
        System.out.println("--------------------------");
        list.forEach(System.out::println);

        //让学生student实现comparable接口，这样也可以给学生排序
        System.out.println("--------------------------");
        List<Student> list1 = new ArrayList<>();
        list1.add(new Student("zq",18));
        list1.add(new Student("ysq",19));
        list1.add(new Student("zxy",20));
        Collections.sort(list1);
        list1.forEach(System.out::println);
    /*    System.out.println("--------------------------");
        for (Student student : list1){
            System.out.println(student);
        }*/
        /*Collections的方法
        统计集合中元素出现的次数
        Collections.frequency(list1,new Student("zq",18));
        //synchronizedList多线程集合
        List<Integer> synchronizedList = Collections.synchronizedList(list);
       */
    }


}
